﻿using IBaseRepository;
using IServices;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Routing;
using SqlSugarAndEntity;
using SqlSugarAndEntity.DataTransferObject.logDTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Core;
using Core.ExpressionHelper;
using Core.CacheHelper;
using Core.CommonModel;
using Core.AutoFac;
using Core.App;
using Core.JwtHelper;

namespace Services
{
    public class LogService: BaseService<sys_loginfo>,ILogService,IAutoInject
    {
        private IBaseRepository<sys_loginfo> logRepository;
        public LogService(IBaseRepository<sys_loginfo> logRepository):base(logRepository)
        {
            this.logRepository = logRepository;
        }

        public List<ControllerDTO> GetControllerName()
        {
            if (!MemoryCacheHelper.Exists("controllerList"))
            {
                List<ControllerDTO> list = new List<ControllerDTO>();
                List<Type> assembly = new List<Type>();
                foreach (var dll in App.GetDlls())
                {
                    var controllers = dll.GetTypes().AsEnumerable().Where(t => typeof(ControllerBase).IsAssignableFrom(t)&& t.GetCustomAttribute(typeof(NotLogAttribute)) == null).ToList();
                    assembly.AddRange(controllers);
                }
                assembly.ForEach(t =>
                {
                    ControllerDTO item = new ControllerDTO();
                    item.name = t.Name.Replace("Controller","");
                    item.value = item.name;
                    item.hasChildren = true;
                    List<ControllerDTO> ChildrenList = new List<ControllerDTO>();
                    foreach (var method in t.GetMethods().Where(a => a.IsPublic && a.GetCustomAttribute(typeof(HttpMethodAttribute)) != null))
                    {
                        var attr = method.GetCustomAttribute<HttpMethodAttribute>();
                        ControllerDTO Children = new ControllerDTO();
                        Children.name = method.Name;
                        Children.value= attr.Template == null ? attr.HttpMethods.FirstOrDefault() : attr.Template;
                        Children.hasChildren = false;
                        ChildrenList.Add(Children);
                    }
                    item.children = ChildrenList;
                    list.Add(item);
                });
                MemoryCacheHelper.SetValue("controllerList", list);
                return list;
            }
            else
            {
                return MemoryCacheHelper.GetTByKey<List<ControllerDTO>>("controllerList");
            }
        }

        public ResponseModel GetOrderbyInfoByPage(DateTime? StartTime, DateTime? EndTime, string Method,string ControllerName, string Action,int page, int limit)
        {
            Expression<Func<sys_loginfo, bool>> orgin = null;
            orgin = orgin.AndIF(!string.IsNullOrEmpty(Method), t => t.RequestMethod == Method.ToUpper())
                .AndIF(StartTime != null, t => t.CreateTime >= StartTime)
                .AndIF(EndTime != null, t => t.CreateTime <= EndTime);
            if(Action=="GET"|| Action == "POST"|| Action == "PUT"|| Action == "DELETE")
            {
                orgin = orgin.And(t => t.RequestMethod == Action)
                    .AndIF(ControllerName != null, t => t.RequestPath.EndsWith(ControllerName));
            }
            else
            {
                orgin=orgin.AndIF(Action!=null,t => t.RequestPath.Contains(Action))
                    .AndIF(ControllerName != null, t => t.RequestPath.Contains(ControllerName));
            }
            return base.GetOrderbyDescInfoByPage(orgin, t => new { t.ID }, page, limit);
        }
    }
}
